import 'package:flutter/material.dart';

import 'CartNum.dart';
import '../../../config/Config.dart';

import 'package:provider/provider.dart';
import '../../../provider/Cart.dart';

class CartItem extends StatefulWidget {
  Map item;
  CartItem({this.item});

  @override
  State<StatefulWidget> createState() => CartItemState(item: this.item);
}

class CartItemState extends State<CartItem> {
  Map item;
  CartItemState({this.item});

  @override
  Widget build(BuildContext context) {
    // 第二次改变时，initState不会刷新，导致数据错误，使用widget进行同步
    this.item = widget.item;

    var cartProvider = Provider.of<Cart>(context);
    String pic = item['pic'];

    return Container(
      margin: EdgeInsets.only(left: 5, top: 5, right: 5),
      padding: EdgeInsets.all(10),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
              bottom: BorderSide(
                color: Colors.black12,
                width: 1,
              ))),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Checkbox(
                  activeColor: Colors.red,
                  value: item['checked'],
                  onChanged: (value) {
                    item['checked'] = !item['checked'];
                    cartProvider.itemCheckedChange();
                  }),
              Container(
                margin: EdgeInsets.only(left: 10,right: 10),
                width: 80,
                height: 80,
                child: Image.network(
                  '${Config.domain + pic.replaceAll('\\', '/')}',
                  fit: BoxFit.fill,
                ),
              ),
              Expanded(
                  flex: 1,
                  child: Container(
                    height: 80,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '${item['title']}',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(fontSize: 12),
                        ),
                        Text('${item['selectedAttr']}', style: TextStyle(fontSize: 12)),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('￥${item['price']}', style: TextStyle(color: Colors.red, fontSize: 12)),
                            // 增加或者减少数量
                            CartNum(item: item),
                          ],
                        )
                      ],
                    ),
                  )
              ),
            ],
          ),
        ],
      ),
    );
  }
}
